package com.ybkj.chat.module;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ybkj.chat.R;
import com.ybkj.chat.base.BaseMvpActivity;
import com.ybkj.chat.manager.ActivityManager;
import com.ybkj.chat.module.friend_center.fragment.FriendFragment;
import com.ybkj.chat.module.main.fragment.DynamicFragment;
import com.ybkj.chat.module.main.fragment.MainFragment;
import com.ybkj.chat.module.main.fragment.MessageFragment;
import com.ybkj.chat.module.main.fragment.MineFragment;
import com.ybkj.chat.utils.ResourcesUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * - @Author:  Yi Shan Xiang
 * - @Description:  首页
 */
public class MainActivity extends BaseMvpActivity<MainPresenter> implements IMainAtView {
    //圈子
    @BindView(R.id.iv_center)
    ImageView ivCenter;
    @BindView(R.id.tv_home)
    TextView tvHome;
    //icon_circle_friend
    @BindView(R.id.iv_friend)
    ImageView ivFriend;
    @BindView(R.id.tv_friend)
    TextView tvFriend;
    //消息
    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    //动态
    @BindView(R.id.iv_dynamic)
    ImageView ivDynamic;
    @BindView(R.id.tv_dynamic)
    TextView tvDynamic;
    //我的
    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.tv_mine)
    TextView tvMine;
    //点击回退的时间
    private long recodeTime = 0;
    private MainFragment mainFragment;
    private FriendFragment friendFragment;
    private MessageFragment messageFragment;
    private DynamicFragment dynamicFragment;
    private MineFragment mineFragment;
    private FragmentTransaction ft;


    @Override
    protected void initTitle() {
        setBottomLineVisibility(View.GONE);
        //隐藏返回键
        setBackImgVisibility(View.GONE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void onRightTextClick() {
        super.onRightTextClick();
    }

    @Override
    protected void initView() {


    }

    @Override
    protected void initData() {
        setSelect(0);
    }

    /**
     * 连续点击2次返回键才能退出
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - recodeTime > 2000) {
                toast(getResources().getString(R.string.click_exit));
                recodeTime = System.currentTimeMillis();
                return true;
            } else {
                //退出程序
                finish();
                ActivityManager.exit();
            }
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void injectPresenter() {
        getActivityComponent().inject(this);
    }


    @OnClick({R.id.ll_center, R.id.ll_friend, R.id.ll_message, R.id.ll_dynamic, R.id.ll_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_center:
                setSelect(0);
                break;
            case R.id.ll_friend:
                setSelect(1);
                break;
            case R.id.ll_message:
                setSelect(2);
                break;
            case R.id.ll_dynamic:
                setSelect(3);
                break;
            case R.id.ll_mine:
                setSelect(4);
                break;
        }
    }

    private void setSelect(int status) {
        FragmentManager fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        hideFragments();
        resetImages();

        switch (status) {
            case 0:
                ivCenter.setImageResource(R.drawable.icon_main_select);
                tvHome.setTextColor(ResourcesUtil.getColor(R.color.color_9ff));

                if (mainFragment == null) {
                    mainFragment = new MainFragment();
                    ft.add(R.id.main_container, mainFragment);
                }
                ft.show(mainFragment);
                break;
            case 1:
                ivFriend.setImageResource(R.drawable.icon_friend_select);
                tvFriend.setTextColor(ResourcesUtil.getColor(R.color.color_9ff));
                if (friendFragment == null) {
                    friendFragment = new FriendFragment();
                    ft.add(R.id.main_container, friendFragment);
                }
                ft.show(friendFragment);
                break;
            case 2:
                if (messageFragment == null) {
                    messageFragment = new MessageFragment();
                    ft.add(R.id.main_container, messageFragment);
                }
                ft.show(messageFragment);
                ivMessage.setImageResource(R.drawable.icon_message_select);
                tvMessage.setTextColor(ResourcesUtil.getColor(R.color.color_9ff));
                break;
            case 3:
                if (dynamicFragment == null) {
                    dynamicFragment = new DynamicFragment();
                    ft.add(R.id.main_container, dynamicFragment);
                }
                ft.show(dynamicFragment);
                ivDynamic.setImageResource(R.drawable.icon_dynamic_select);
                tvDynamic.setTextColor(ResourcesUtil.getColor(R.color.color_9ff));
                break;
            case 4:
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                    ft.add(R.id.main_container, mineFragment);
                }
                ft.show(mineFragment);
                ivMine.setImageResource(R.drawable.icon_mine_select);
                tvMine.setTextColor(ResourcesUtil.getColor(R.color.color_9ff));
                break;
        }
        ft.commit();
    }

    private void hideFragments() {
        if (mainFragment != null) {
            ft.hide(mainFragment);
        }
        if (friendFragment != null) {
            ft.hide(friendFragment);
        }
        if (messageFragment != null) {
            ft.hide(messageFragment);
        }
        if (dynamicFragment != null) {
            ft.hide(dynamicFragment);
        }
        if (mineFragment != null) {
            ft.hide(mineFragment);
        }
    }


    private void resetImages() {
        ivCenter.setImageResource(R.drawable.icon_main);
        tvHome.setTextColor(ResourcesUtil.getColor(R.color.color_gray_2));
        ivFriend.setImageResource(R.drawable.icon_friend);
        tvFriend.setTextColor(ResourcesUtil.getColor(R.color.color_gray_2));
        ivMessage.setImageResource(R.drawable.icon_message);
        tvMessage.setTextColor(ResourcesUtil.getColor(R.color.color_gray_2));
        ivDynamic.setImageResource(R.drawable.icon_dynamic);
        tvDynamic.setTextColor(ResourcesUtil.getColor(R.color.color_gray_2));
        ivMine.setImageResource(R.drawable.icon_mine);
        tvMine.setTextColor(ResourcesUtil.getColor(R.color.color_gray_2));
    }

    @Override
    public boolean isImmersiveStatusBar() {
        return true;
    }
}
